package de.lot.action;

import de.lot.model.LearningOutcome;
import de.lot.model.ProgramGoal;
import de.lot.model.Trait;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.web.RequestParameter;
import org.jboss.seam.framework.EntityHome;

/**
 * Class representing the form to view an LearningOutcome
 * 
 * @see LearningOutcome
 * @author Stefan Kohler <kohler.stefan@gmail.com>
 */
@Name("learningOutcomeHome")
public class LearningOutcomeHome extends EntityHome<LearningOutcome> {

    @In(create = true)
    ProgramGoalHome programGoalHome;
    @RequestParameter
    Long learningOutcomeId;

    @Override
    public Object getId() {
        if (learningOutcomeId == null) {
            return super.getId();
        } else {
            return learningOutcomeId;
        }
    }

    public void setLearningOutcomeId(Long id) {
        setId(id);
    }

    public Long getLearningOutcomeId() {
        return (Long) getId();
    }

    @Override
    protected LearningOutcome createInstance() {
        LearningOutcome learningOutcome = new LearningOutcome();
        return learningOutcome;
    }

    @Override
    @Begin(join = true)
    public void create() {
        super.create();
    }

    /**
     * Sets the programGoal of the actual learningOutcome instance
     */
    public void wire() {
        getInstance();
        ProgramGoal programGoal = programGoalHome.getDefinedInstance();
        if (programGoal != null) {
            getInstance().setProgramGoal(programGoal);
        }
    }

    /**
     * Checks if programGoal of learningOutcome is set.
     * 
     * @return <code>true</code> if LearningOutcome is associated with a
     *         ProgramGoal
     *         <code>false</code> if LearningOutcome is NOT associated with a
     *         ProgramGoal
     * @see ProgramGoal
     */
    public boolean isWired() {
        if (getInstance().getProgramGoal() == null) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * Gets the defined instance - if id is set.
     *
     * @return <code>learingOutcome</code> if the <code>id</code> is defined
     *         <code>null</code> if <code>id</code> is not defined
     */
    public LearningOutcome getDefinedInstance() {
        return isIdDefined() ? getInstance() : null;
    }

    /**
     * A list of all traits belonging to the LO
     *
     * @see Trait
     * @return List<Trait> traits belonging to LO
     */
    public List<Trait> getTraits() {
        if (getInstance() == null) {
            return null;
        }
        List<Trait> traits = new ArrayList<Trait>(getInstance().getTraitList());
        Collections.sort(traits, new Comparator<Trait>() {

            public int compare(Trait a, Trait b) {
                return Integer.valueOf(a.getNumber().compareTo(Integer.valueOf(b.getNumber())));
            }
        });
        return traits;
    }
}
